﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HotelManager.Data_Transfer_Layer;
using HotelManager.Business_Logic_Layer;

namespace HotelManager
{
    public partial class QuanLyDichVu : UserControl
    {
        loaidichvuBLO ldv_blo;
        dichvuBLO dv_blo;
        private int option;
        public QuanLyDichVu()
        {
            InitializeComponent();
            ldv_blo = new loaidichvuBLO();
            dv_blo = new dichvuBLO();
            option = 0;
        }
        public void Load_gridview(int opt)
        {
            dichvuDTO[] list_dv = new dichvuDTO[dv_blo.get().Count()];
            if (opt == 1)
            {
                list_dv = dv_blo.get();
            }
            else
            {
                string dv_tendv = cbbLoc.Text;
                int ldv_id = ldv_blo.View_id(dv_tendv);
                list_dv = dv_blo.List_current(ldv_id);
            }
            // DataTable
            DataTable dt = new DataTable();
            DataColumn tendv = new DataColumn();
            tendv.ColumnName = "tendv";
            tendv.ReadOnly = true;
            DataColumn donvi = new DataColumn();
            donvi.ColumnName = "donvi";
            donvi.ReadOnly = true;
            DataColumn soluong = new DataColumn();
            soluong.ColumnName = "soluong";
            soluong.ReadOnly = true;
            DataColumn gia = new DataColumn();
            gia.ColumnName = "gia";
            gia.ReadOnly = true;
            DataColumn loaidichvu = new DataColumn();
            loaidichvu.ColumnName = "loaidichvu";
            loaidichvu.ReadOnly = true;
            DataColumn id = new DataColumn();
            id.ColumnName = "id";
            id.ReadOnly = true;
            //
            dt.Columns.Add(tendv);
            dt.Columns.Add(donvi);
            dt.Columns.Add(soluong);
            dt.Columns.Add(gia);
            dt.Columns.Add(loaidichvu);
            dt.Columns.Add(id);
            //
            foreach (dichvuDTO obj in list_dv)
            {
                DataRow row = dt.NewRow();
                row["tendv"] = obj.Dv_tendv;
                row["donvi"] = obj.Dv_donvi;
                int ldv_id = obj.Ldv_id;
                loaidichvuDTO ldv_dto = new loaidichvuDTO();
                ldv_dto = ldv_blo.View_Detail(ldv_id);
                row["soluong"] = obj.Dv_soluong.ToString();
                row["gia"] = obj.Dv_gia.ToString();
                row["loaidichvu"] = ldv_dto.Ldv_tenloaidv;
                row["id"] = obj.Dv_id;
                dt.Rows.Add(row);
            }
            //
            dgvData.DataSource = dt;

            dgvData.Columns[0].Width = 50;
            dgvData.Columns[1].Width = 50;
            dgvData.Columns[2].HeaderText = "Tên dịch vụ";
            dgvData.Columns[3].HeaderText = "Đơn vị";
            dgvData.Columns[4].HeaderText = "Số lượng";
            dgvData.Columns[5].HeaderText = "Giá";
            dgvData.Columns[6].HeaderText = "Loại dịch vụ";
            dgvData.Columns[7].Visible = false;
        }

        public void Load_Data()
        {
            cbbLoc.DataSource = ldv_blo.get_arr();
            cbbLoai.DataSource = ldv_blo.get_arr();
        }

        public void truyendulieu()
        {
            Load_Data();
        }

        private void QuanLyDichVu_Load(object sender, EventArgs e)
        {
            Load_Data();
            Load_gridview(1);
        }

        private void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                int dv_id = int.Parse(dgvData.Rows[e.RowIndex].Cells[7].Value.ToString());
                dichvuDTO obj = new dichvuDTO();
                obj = dv_blo.View_Detail(dv_id);
                DialogResult result = MessageBox.Show("Bạn có muốn xoá " + obj.Dv_tendv + "?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    MessageBox.Show("Ban chon yes");

                }
                else
                {
                    MessageBox.Show("Ban chon no");
                }

            }
            if (e.ColumnIndex == 1)
            {
                int dv_id = int.Parse(dgvData.Rows[e.RowIndex].Cells[7].Value.ToString());
                dichvuDTO obj = new dichvuDTO();
                obj = dv_blo.View_Detail(dv_id);
                txtTenDV.Text = obj.Dv_tendv;
                txtSoLuong.Text = obj.Dv_soluong.ToString();
                txtGiaban.Text = obj.Dv_gia.ToString();
                cbbDonvi.Text = obj.Dv_donvi;
                cbbLoai.Text=dgvData.Rows[e.RowIndex].Cells[6].Value.ToString();
                lblID.Text = dv_id.ToString();
                option = 1;
            }
        }

        private void cbbLoc_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            string dv_tendv = cbbLoc.Text;
            int ldv_id = ldv_blo.View_id(dv_tendv);
            if (ldv_id != 1)
                Load_gridview(2);
            else
                Load_gridview(1);
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            if (txtGiaban.Text.Length == 0 || txtSoLuong.Text.Length == 0 || txtTenDV.Text.Length == 0||cbbDonvi.Text==""||cbbLoai.Text=="")
            {
                MessageBox.Show("Vui lòng nhập thông tin đầy đủ", "Lỗi");
                return;
            }
            DialogResult result = MessageBox.Show("Bạn có muốn cập nhật " + txtTenDV.Text + "?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (option == 1) 
                {
                    dichvuDTO obj = new dichvuDTO();
                    obj.Dv_id = int.Parse(lblID.Text);
                    obj.Dv_tendv = txtTenDV.Text;
                    obj.Dv_soluong = int.Parse(txtSoLuong.Text);
                    obj.Dv_gia = int.Parse(txtGiaban.Text);
                    obj.Dv_donvi = cbbDonvi.Text;
                    obj.Ldv_id = ldv_blo.View_id(cbbLoai.Text);
                    dv_blo.Update(obj);
                    MessageBox.Show("Cập nhật thành công!");
                    Load_gridview(1);
                }
                // Update
                else 
                {
                    dichvuDTO obj = new dichvuDTO();
                    obj.Dv_tendv = txtTenDV.Text;
                    obj.Dv_soluong = int.Parse(txtSoLuong.Text);
                    obj.Dv_gia = int.Parse(txtGiaban.Text);
                    obj.Dv_donvi = cbbDonvi.Text;
                    obj.Ldv_id = ldv_blo.View_id(cbbLoai.Text);
                    dv_blo.Insert(obj);
                    MessageBox.Show("Cập nhật thành công!");
                    Load_gridview(1);
                }
                    // Create
            }
            else
            {

                MessageBox.Show("Ban chon no");
            }
        }

        private void txtTenDV_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtGiaban_TextChanged(object sender, EventArgs e)
        {

        }

        private void cbbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbbDonvi_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaban_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        

    }
}
